﻿using CoreShop.Model;
using CoreShop.Repository;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using System.Text;
using System.Threading.Tasks;

namespace CoreShop.Services
{
    public class corecmsexchangeServices : Repository<corecmsexchange>, IcorecmsexchangeServices
    {
        /// <summary>
        /// 获取缓存的所有数据
        /// </summary>
        /// <returns></returns>
        public List<corecmsexchange> GetCaChe()
        {
            return base.GetList();
        }
        public corecmsexchange GetById(long id)
        {
            //return base.Change<corecmsexchange>().GetById(id);
            return base.GetSingle(a => a.storeId == id);
        }

        public corecmsexchange GetDataById(long storeId)
        {            
            var exchange = base.GetSingle(a => a.storeId == storeId);
            if (exchange == null)
            {
                exchange = new corecmsexchange();
                exchange.storeId = storeId;
                base.InsertAsync(exchange);                
            }
            return exchange;
        }

        public bool InsertAsync(corecmsexchange entity)
        {
            return base.Insert(entity);
        }

        /// <summary>
        /// 重写异步更新方法方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Updateable(corecmsexchange entity)
        {
            return Context.Updateable(entity).Where(p => p.storeId == entity.storeId).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 重写异步更新方法方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(corecmsexchange entity)
        {
            if (!string.IsNullOrEmpty(entity.content))
            {
               return base.Change<corecmsexchange>().Update(a => new corecmsexchange()
                {
                    content = entity.content,
                }, a => a.storeId == entity.storeId);
            }

            if (entity.foodId > 0)
            {
                return base.Change<corecmsexchange>().Update(a => new corecmsexchange()
                {
                    foodId = entity.foodId,
                    foodTime = entity.foodTime,
                }, a => a.storeId == entity.storeId);
            }
            if (entity.foodCategoryId > 0)
            {
                return base.Change<corecmsexchange>().Update(a => new corecmsexchange()
                {
                    foodCategoryId = entity.foodCategoryId,
                    foodCategoryTime = entity.foodCategoryTime,
                }, a => a.storeId == entity.storeId);
            }

            if (entity.activityId > 0)
            {
                return base.Change<corecmsexchange>().Update(a => new corecmsexchange()
                {
                    activityId = entity.activityId,
                    activityTime = entity.activityTime
                }, a => a.storeId == entity.storeId);
            }
            if (entity.foodSpecId > 0)
            {
                return base.Change<corecmsexchange>().Update(a => new corecmsexchange()
                {
                    foodSpecId = entity.foodSpecId,
                    foodSpecTime = entity.foodSpecTime
                }, a => a.storeId == entity.storeId);
            }

            if (entity.sysUserId > 0)
            {
                return base.Change<corecmsexchange>().Update(a => new corecmsexchange()
                {
                    sysUserId = entity.sysUserId,
                    sysUserTime = entity.sysUserTime
                }, a => a.storeId == entity.storeId);
            }
            if (entity.screemImgId > 0)
            {
                return base.Change<corecmsexchange>().Update(a => new corecmsexchange()
                {
                    screemImgId = entity.screemImgId,
                    screemImgTime = entity.screemImgTime
                }, a => a.storeId == entity.storeId);
            }
            if (entity.activityId > 0)
            {
                return base.Change<corecmsexchange>().Update(a => new corecmsexchange()
                {
                    activityId = entity.activityId,
                    activityTime = entity.activityTime
                }, a => a.storeId == entity.storeId);
            }
            if (entity.employeeId > 0)
            {
                return base.Change<corecmsexchange>().Update(a => new corecmsexchange()
                {
                    employeeId = entity.employeeId,
                    employeeTime = entity.employeeTime
                }, a => a.storeId == entity.storeId);
            }
            if (entity.chgPriceId > 0)
            {
                return base.Change<corecmsexchange>().Update(a => new corecmsexchange()
                {
                    chgPriceId = entity.chgPriceId,
                    chgPriceTime = entity.chgPriceTime
                }, a => a.storeId == entity.storeId);
            }
            if (entity.branchPriceId > 0)
            {
                return base.Change<corecmsexchange>().Update(a => new corecmsexchange()
                {
                    branchPriceId = entity.branchPriceId,
                    branchPriceTime = entity.branchPriceTime
                }, a => a.storeId == entity.storeId);
            }
            if (entity.foodUnitId > 0)
            {
                return base.Change<corecmsexchange>().Update(a => new corecmsexchange()
                {
                    foodUnitId = entity.foodUnitId,
                    foodUnitTime = entity.foodUnitTime
                }, a => a.storeId == entity.storeId);
            }
            return true;
        }
    
    }
}
